package com.hoki.zj.hrm.controller;

import com.hoki.zj.hrm.domain.Tenant;
import com.hoki.zj.hrm.dto.EnteringDto;
import com.hoki.zj.hrm.query.TenantQuery;
import com.hoki.zj.hrm.service.ITenantService;
import com.hoki.zj.hrm.utils.AjaxResult;
import com.hoki.zj.hrm.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Autowired
    public ITenantService tenantService;


    /**
     * 保存和修改公用的
     *
     * @param tenant 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant) {
        try {
            if (tenant.getId() != null)
                tenantService.updateById(tenant);
            else
                tenantService.insert(tenant);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            tenantService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Tenant get(@PathVariable("id") Long id) {
        return tenantService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Tenant> list() {

        return tenantService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Tenant> json(@RequestBody TenantQuery query) {
        return tenantService.selectPagelist(query);
    }

    @PostMapping("/entering")
    public AjaxResult entering(@RequestBody @Valid EnteringDto dto ) { // @Valid 对接收的参数进行验证,嵌套验证必须是用Valid注解
        tenantService.entring(dto);
        return AjaxResult.me();
    }
}
